package cn.itsource.controller;

import cn.itsource.domain.ArticleType;
import cn.itsource.query.ArticleTypeQuery;
import cn.itsource.service.ArticleTypeService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：文章类型ArticleType
 */
@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {

    @Autowired
    private ArticleTypeService articleTypeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<ArticleType> findAll(){
        return articleTypeService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ArticleType findOne(@PathVariable("id")Long id){
        return articleTypeService.findOne(id);
    }

    /**
     * 添加和修改
     * @param articleType
     * @return
     */
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody ArticleType articleType){
        try {
            if(articleType.getId()==null){//添加操作
                articleTypeService.add(articleType);
            }else{
                articleTypeService.update(articleType);
            }
            //如果成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            articleTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 分页查询 + 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public PageInfo<ArticleType> pageQuery(@RequestBody ArticleTypeQuery query){
        return articleTypeService.pageQuery(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            articleTypeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

}
